package com.csp.utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liupengchao on 16/10/17.
 */
public class JsonHelper {


    private static Logger log = LoggerFactory.getLogger(JsonHelper.class);

    /**
     * 将不含日期时间格式的Java对象系列化为Json资料格式
     *
     * @param pObject
     *            传入的Java对象
     * @return
     */
    public static final String encodeObject2Json(Object pObject) {
        String jsonString = "[]";
        if (AAUtils.isEmpty(pObject)) {
            // log.warn("传入的Java对象为空,不能将其序列化为Json资料格式.请检查!");
        } else {
            if (pObject instanceof ArrayList) {
                JSONArray jsonArray = JSONArray.fromObject(pObject);
                jsonString = jsonArray.toString();
            } else {
                JSONObject jsonObject = JSONObject.fromObject(pObject);
                jsonString = jsonObject.toString();

            }
        }
       /* if (log.isInfoEnabled()) {
            log.info("序列化后的JSON资料输出:\n" + jsonString);
        }*/
        return jsonString;
    }

    /**
     * 将含有日期时间格式的Java对象系列化为Json资料格式<br>
     * Json-Lib在处理日期时间格式是需要实现其JsonValueProcessor接口,所以在这里提供一个重载的方法对含有<br>
     * 日期时间格式的Java对象进行序列化
     *
     * @param pObject
     *            传入的Java对象
     * @return
     */
    public static final String encodeObject2Json(Object pObject, String pFormatString) {
        String jsonString = "[]";
        if (AAUtils.isEmpty(pObject)) {
            // log.warn("传入的Java对象为空,不能将其序列化为Json资料格式.请检查!");
        } else {
            if (pObject instanceof ArrayList) {
                JSONArray jsonArray = JSONArray.fromObject(pObject, null);
                jsonString = jsonArray.toString();
            } else {
                JSONObject jsonObject = JSONObject.fromObject(pObject, null);
                jsonString = jsonObject.toString();
            }
        }
       /* if (log.isInfoEnabled()) {
            log.info("序列化后的JSON资料输出:\n" + jsonString);
        }*/
        return jsonString;
    }


    /**
     * 用于web
     * 此类内部使用,不对外暴露
     * @return 返回合并后的字符串
     */
    public static String encodeJson_extr(Object pObject) {
        JSONObject jsonObject = JSONObject.fromObject(pObject);
        String jsonString = jsonObject.toString();
        jsonString = "["+ jsonString + "]";
        if (log.isInfoEnabled()) {
            log.info("合并后的JSON资料输出:\n" + jsonString);
        }
        return jsonString;
    }


    /**
     * 将分页信息压入JSON字符串
     * 此类内部使用,不对外暴露
     * @return 返回合并后的字符串
     */
    public static String encodeJson2PageJson(String jsonString) {
        jsonString = "{\"rows\":" + jsonString + "}";
        /*if (log.isInfoEnabled()) {
            log.info("合并后的JSON资料输出:\n" + jsonString);
        }*/
        return jsonString;
    }



    /**
     * 将分页信息压入JSON字符串
     * 此类内部使用,不对外暴露
     * @param totalCount
     * @return 返回合并后的字符串
     */
    public static String encodeJson2PageJson(String jsonString, int totalCount) {
        jsonString = "{\"rows\":" + jsonString + ", \"totalcount\":" + totalCount + "}";
       /* if (log.isInfoEnabled()) {
            log.info("合并后的JSON资料输出:\n" + jsonString);
        }*/
        return jsonString;
    }

    /**
     * 将分页信息压入JSON字符串
     * 此类内部使用,不对外暴露
     * @param totalCount
     * @return 返回合并后的字符串
     */
    public static String encodeJson2PageJson2(String jsonString, int totalCount,int inoutcount) {
        //jsonString = "{\"rows\":" + jsonString + ", \"totalcount\":" + totalCount + ",\"monthinoutcount\":" + inoutcount+"}";

        jsonString = "{\"rows\":" + jsonString + ", \"totalcount\":" + totalCount+ "}";
       /* if (log.isInfoEnabled()) {
            log.info("合并后的JSON资料输出:\n" + jsonString);
        }*/
        return jsonString;
    }


    /**
     * 直接将List转为分页所需要的Json资料格式
     *
     * @param list
     *            需要编码的List对象
     * @param totalCount
     *            记录总数
     *            时间日期格式化,传null则表明List不包含日期时间属性
     */
    public static final String encodeList2PageJsonForCurrent(List list, Integer totalCount, Integer currentPage, Integer totalPage) {

        String	subJsonString = encodeObject2Json(list);

        String jsonString = "{\"total\":" + totalCount + ",\"currentpage\":"+currentPage+",\"totalpage\":"+totalPage+",\"data\":" + subJsonString + "}";
        return jsonString;
    }


    /**
     * 直接将List转为分页所需要的Json资料格式
     *
     * @param list
     *            需要编码的List对象
     * @param totalCount
     *            记录总数
     *            时间日期格式化,传null则表明List不包含日期时间属性
     */
    public static final String encodeList2PageJson(List list, Integer totalCount, String dataFormat) {
        String subJsonString = "";
        if (AAUtils.isEmpty(dataFormat)) {
            subJsonString = encodeObject2Json(list);
        } else {
            subJsonString = encodeObject2Json(list, dataFormat);
        }
        String jsonString = "{\"success\":" + totalCount + ", \"data\":" + subJsonString + "}";
        return jsonString;
    }


    /**
     * 用于下拉
     * @param list
     * @param totalCount
     * @param dataFormat
     * @return
     */
    public static final String encodeList2SelectJson(List list, Integer totalCount, String dataFormat,boolean flag) {
        String subJsonString = "";
        if (AAUtils.isEmpty(dataFormat)) {
            subJsonString = encodeObject2Json(list);
        } else {
            subJsonString = encodeObject2Json(list, dataFormat);
        }
        String jsonString = "{\"total_count\":" + totalCount + ",\"incomplete_results\":" + flag + ", \"items\":" + subJsonString + "}";
        return jsonString;
    }




    /**
     * 直接将List转为分页所需要的Json资料格式
     *
     * @param list
     *            需要编码的List对象
     * @param totalCount
     *            记录总数
     *            时间日期格式化,传null则表明List不包含日期时间属性
     */
    public static final String encodeList2PageJsonForA(List list, Integer totalCount, String dataFormat) {
        String subJsonString = "";
        if (AAUtils.isEmpty(dataFormat)) {
            subJsonString = encodeObject2Json(list);
        } else {
            subJsonString = encodeObject2Json(list, dataFormat);
        }
        String jsonString = "{\"aaData\":" + subJsonString + "}";
        return jsonString;
    }



    /**
     *
     * @Title: encodeList2PageJsonForDataTable
     * @Description: TODO()
     * @param @param list
     * @param @param draw
     * @param @param totalCount
     * @param @param recordsFiltered
     * @param @param dataFormat
     * @param @return
     * @return String
     * @throws
     */
    public static final String encodeList2PageJsonForDataTable(List list,String draw,Integer totalCount,Integer recordsFiltered,String dataFormat) {
        String subJsonString = "";
        if (AAUtils.isEmpty(dataFormat)) {
            subJsonString = encodeObject2Json(list);
        } else {
            subJsonString = encodeObject2Json(list, dataFormat);
        }
        String jsonString = "{\"draw\":" + draw + ",\"recordsTotal\":"+totalCount+ ",\"recordsFiltered\":"+recordsFiltered+ ",\"data\":" + subJsonString + "}";
        return jsonString;
    }


}
